﻿/**
 ** @作者:	   王桂龙 QQ731513122
 ** @日期:	   2019.12.6
 ** @简介:     GroupBox      
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BHXJDOTNET.OLD
{
    public partial class BHXJGroupBox : GroupBox,IBHXJZOOM,IBHXJCONTAINER
    {
        private Color _边框颜色 = Color.FromArgb(64, 64, 64);
        public Color 边框颜色
        {
            get
            {
                return this._边框颜色;
            }
            set
            {
                if (this._边框颜色 != value)
                {
                    this._边框颜色 = value;
                    this.Invalidate();
                }
            }
        }


        #region 继承于ZOOM接口的属性和方法
        private Point _OriginPoint = new Point(0, 0);
        [Description("原点(慎用,在足够了解BHXJZoom的情况下使用)"), Category("BHXJZoom")]
        /// <summary>
        /// 原点
        /// </summary>
        public Point OriginPoint
        {
            get
            { return this._OriginPoint; }
            set
            {
                if (this._OriginPoint != value)
                {
                    this._OriginPoint = value;
                    if (DesignMode)
                        this.IniLocation = this.BHXJGetIniLocationInDesignMode();
                }
            }
        }
        private bool _AutoOrigin = false;
        [Description("自动更改原点模式(只在设计模式下有用)"), Category("BHXJZoom")]
        /// <summary>
        /// 自动更改原点模式(只在设计模式下有用)
        /// </summary>
        public bool AutoOrigin
        {
            get
            {
                return this._AutoOrigin;
            }
            set
            {
                if (this._AutoOrigin != value)
                {
                    this._AutoOrigin = value;
                    if (DesignMode)
                    {
                        if (this.AutoOrigin)
                        {
                            this.OriginPoint = this.Location;
                        }
                        else
                        {
                            this.OriginPoint = new Point(0, 0);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 初始尺寸
        /// </summary>
        [Browsable(false)]
        public Size IniSize { get; set; }
        /// <summary>
        /// 初始位置
        /// </summary>
        [Browsable(false)]
        public Point IniLocation { get; set; }
        /// <summary>
        /// 初始字体
        /// </summary>
        [Browsable(false)]
        public Font IniFont { get; set; }

        #endregion

        #region 内部IZoom控件缩放
        private bool _ZoomFlag = true;
        /// <summary>
        /// 是否对子控件启用Zoom功能
        /// </summary>
        public bool ZoomFlag
        {
            get { return this._ZoomFlag; }
            set
            {
                if (value != this._ZoomFlag)
                {
                    this._ZoomFlag = value;
                    //重新缩放
                    this.Control_SizeChanged(this, null);
                }
            }
        }

        private bool _ZoomSize = true;
        /// <summary>
        /// （ZoomFlag为真时）缩放子控件尺寸
        /// </summary>
        public bool ZoomSize
        {
            get { return this._ZoomSize; }
            set
            {
                if (value != this._ZoomSize)
                {
                    this._ZoomSize = value;
                    //重新缩放
                    this.Control_SizeChanged(this, null);
                }
            }
        }

        private bool _ZoomFont = true;
        /// <summary>
        /// （ZoomFlag为真时）缩放子控件字体
        /// </summary>
        public bool ZoomFont
        {
            get { return this._ZoomFont; }
            set
            {
                if (value != this._ZoomFont)
                {
                    this._ZoomFont = value;
                    //重新缩放
                    this.Control_SizeChanged(this, null);
                }
            }
        }

        private bool _ZoomLocation = true;
        /// <summary>
        /// （ZoomFlag为真时）缩放子控件位置
        /// </summary>
        public bool ZoomLocation
        {
            get { return this._ZoomLocation; }
            set
            {
                if (value != this._ZoomLocation)
                {
                    this._ZoomLocation = value;
                    //重新缩放
                    this.Control_SizeChanged(this, null);
                }
            }
        }
        #endregion

        /// <summary>
        /// 内部尺寸
        /// </summary>
        [Browsable(false)]
        public Size InnerSize
        {
            get;
            set;
        }

        public BHXJGroupBox()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.ForeColor = Color.Lime;


            #region 设计时,变化事件
            if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
            {
                //设计时事件
                this.SizeChanged += new EventHandler(this.BHXJ_SizeChangedInDesignMode);
                this.FontChanged += new EventHandler(this.BHXJ_FontChangedInDesignMode);
                this.LocationChanged += new EventHandler(this.BHXJ_LocationChangedInDesignMode);

                //innerSize类
                this.SizeChanged += new EventHandler(this.SizeChangedInner);
            }
            #endregion

            //防止字体在设计器中添加该控件时候自动追随父控件
            this.Font = this.Font;
            this.BackColor = this.BackColor;

            //设置初始
            this.IniSize = this.Size;
            this.IniFont = this.Font;
            this.IniLocation = this.BHXJGetIniLocationInDesignMode();


            this.InnerSize = this.Size;
            //尺寸变化事件
            this.SizeChanged += this.Control_SizeChanged;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Rectangle disArea = this.DisplayRectangle;
            Graphics gs = e.Graphics;
            gs.Clear(this.Parent.BackColor);

            Brush textBrush = new SolidBrush(this.ForeColor);
            Pen edge = new Pen(this._边框颜色);
            SolidBrush BackBrush = new SolidBrush(this.BackColor);

            int lineheight = disArea.Location.Y / 2;

            //背景

            gs.FillRectangle(BackBrush, 0, lineheight, this.Width, this.Height - lineheight);


            

            //文字
            SizeF textarea = e.Graphics.MeasureString(this.Text, this.Font);
            StringFormat s = new StringFormat();
            s.LineAlignment = StringAlignment.Center;
            s.Alignment = StringAlignment.Center;
            int textlocationy = (int)((disArea.Location.Y - textarea.Height) / 2);

            RectangleF rf= new RectangleF(textarea.Height/2, textlocationy, textarea.Width, textarea.Height);
            gs.FillRectangle(new SolidBrush(Color.Transparent), rf);

            e.Graphics.DrawString(this.Text, this.Font, textBrush, rf, s);

            //边框       
            gs.DrawLine(edge, 0, lineheight, rf.Location.X, lineheight);
            gs.DrawLine(edge, rf.Location.X+rf.Width, lineheight, this.Width - 1, lineheight);
            gs.DrawLine(edge, 0, lineheight, 0, this.Height - 1);
            gs.DrawLine(edge, 0, this.Height - 1, this.Width - 1, this.Height - 1);
            gs.DrawLine(edge, this.Width - 1, lineheight, this.Width - 1, this.Height - 1);
        }

        /// <summary>
        /// 尺寸变化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Control_SizeChanged(object sender, EventArgs e)
        {
            if (!this._ZoomFlag)
                return;
            if (this.DesignMode)
                return;
            float inerwith = this.InnerSize.Width;
            float inerhigh = this.InnerSize.Height;
            float scale = (((float)this.Width) / inerwith > ((float)this.Height) / inerhigh) ? ((float)this.Height) / inerhigh : ((float)this.Width) / inerwith;
            int x = (int)(((float)this.Width - scale * inerwith) / 2f);
            int y = (int)(((float)this.Height - scale * inerhigh) / 2f);
            Point p = new Point(x, y);

            if (!(scale > 0))
                return;
            //重置控件原点，并按照比例放大控件
            foreach (Control con in this.Controls)
            {
                if (con.Dock == DockStyle.None)
                {
                    if (con is IBHXJZOOM)
                    {
                        IBHXJZOOM zoom = con as IBHXJZOOM;
                        zoom.OriginPoint = p;
                        if (this._ZoomLocation)
                            zoom.BHXJLocationZoom(scale);
                        else
                            zoom.BHXJLocationZoom(1);
                        if (this._ZoomSize)
                            zoom.BHXJSizeZoom(scale);
                        else
                            zoom.BHXJSizeZoom(1);
                        if (this._ZoomFont)
                            zoom.BHXJFontZoom(scale);
                        else
                            zoom.BHXJFontZoom(1);
                    }
                }
            }
        }


        /// <summary>
        /// 尺寸变化处理函数(设计时用)
        /// </summary>
        /// <param name="bhxjzoom"></param>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SizeChangedInner(object sender, EventArgs e)
        {
            if (this.DesignMode)
            {
                this.InnerSize = this.Size;
            }
        }
    }
}
